/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.DateUtils;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.utils.excel.ExportExcel;
import com.yuhanginfo.modules.sys.entity.Dict;
import com.yuhanginfo.modules.sys.entity.Goods;
import com.yuhanginfo.modules.sys.entity.GoodsStoreManger;
import com.yuhanginfo.modules.sys.entity.Materiel;
import com.yuhanginfo.modules.sys.entity.Store;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.entity.Vender;
import com.yuhanginfo.modules.sys.service.GoodsStoreMangerService;
import com.yuhanginfo.modules.sys.service.MaterielService;
import com.yuhanginfo.modules.sys.service.StoreService;
import com.yuhanginfo.modules.sys.service.VenderService;
import com.yuhanginfo.modules.sys.utils.DictUtils;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 门店商品信息管理Controller
 * 
 * @author yinhao
 * @version 2017-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/goodsstoremanger/")
public class GoodsStoreMangerController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(GoodsStoreMangerController.class);
	
	@Autowired
	private  GoodsStoreMangerService goodsStoreMangerService;
	
	@Autowired
	private MaterielService materielService;
	
	@Autowired
	private StoreService storeService;
	
	@Autowired
	private VenderService venderService;
	
	private static final  String STORELIST = "storeList";
	
	@ModelAttribute
	public Goods get(@RequestParam(required = false) String id) {
		Goods entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = goodsStoreMangerService.get(id);
		}
		if (entity == null) {
			entity = new Goods();
		}
		return entity;
	}

	@RequiresPermissions("sys:goodsstoremanger:view")
	@RequestMapping(value = { "list", "" })
	public String list(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if(goods.getCreateBy() == null){
			goods.setCreateBy(user);
		}
		Page<Goods> page = goodsStoreMangerService.findPage(new Page<Goods>(request, response), goods);
		//商品审核状态
		List<Dict> goodsExamineStatusList = DictUtils.getDictList("goods_examine_status");
		//商品来源
		List<Dict> goodsourceList = DictUtils.getDictList("goods_source");
		//所属门店
		List<Store> storeList = storeService.findStoreBySysUser(user);
		//制造商品牌
		List<Vender> venderList = venderService.findList(new Vender());
		model.addAttribute("goodsExamineStatusList", goodsExamineStatusList);
		model.addAttribute("goodsourceList", goodsourceList);
		model.addAttribute(STORELIST, storeList);
		model.addAttribute("venderList",venderList);
		model.addAttribute("page", page);
		return "modules/sys/goodsstoremanger/goodsstoremangerList";
	}

	@RequiresPermissions("sys:goodsstoremanger:view")
	@RequestMapping(value = "form")
	public String form(Goods goods, Model model) {
		User user = UserUtils.getUser();
		Materiel materiel = new Materiel();
		Goods resultGoods = new Goods();
		if( goods.getId() != null){
			resultGoods = goodsStoreMangerService.get(goods);
			Materiel queryMateriel = new Materiel();
			queryMateriel.setCode(resultGoods.getMaterielCode());
			materiel = materielService.get(queryMateriel);
		}
		//所属门店
		List<Store> storeList = storeService.findStoreBySysUser(user);
		model.addAttribute("goods", resultGoods);
		model.addAttribute("materiel", materiel);
		model.addAttribute(STORELIST, storeList);
		return "modules/sys/goodsstoremanger/goodsstoremangerForm";
	}

	@RequiresPermissions("sys:goodsstoremanger:edit")
	@RequestMapping(value = "save")
	public String save(Goods goods, Model model, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes, HttpSession session) {
		if (!beanValidator(model, goods)) {
			return form(goods, model);
		}
		String[] imageSelect = request.getParameterValues("imageSelect");
		String[] detailSelect = request.getParameterValues("detailSelect");
		String[] serviceSelect = request.getParameterValues("serviceSelect");
		String image = (String) session.getAttribute("image");
		session.removeAttribute("image");
		String detail = (String) session.getAttribute("detail");
		session.removeAttribute("detail");
		String service = (String) session.getAttribute("service");
		session.removeAttribute("service");
		try {
			goodsStoreMangerService.save(UserUtils.getUser().getId(), goods, image, detail, service,imageSelect,detailSelect,serviceSelect);
			Map<String,Object> resultMap = new HashMap<>();
			resultMap.put("resultCode", 1);
			resultMap.put("resultMessage", "保存成功");
			model.addAllAttributes(resultMap);
		} catch (Exception ex) {
			logger.info("save",ex);
		}
		model.addAttribute(new Goods());
		return this.list(new Goods(), request, response, model);
	}

	@RequiresPermissions("sys:goodsstoremanger:edit")
	@RequestMapping(value = "examine")
	public String examine(Goods goods, Model model, HttpServletRequest request, HttpServletResponse response) {
		try {
			goodsStoreMangerService.examine(goods);
		} catch (Exception ex) {
			logger.info("examine",ex);
		}
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsstoremanger:edit")
	@RequestMapping(value = "batchExamine")
	public String batchExamine(Goods goods,String[] arrayId, Model model, HttpServletRequest request, HttpServletResponse response) {
		try {
			goodsStoreMangerService.batchExamine(arrayId);
		} catch (Exception ex) {
			logger.info("batchExamine",ex);
		}
		return this.list(goods, request, response, model);
	}
	
	@RequiresPermissions("sys:goodsstoremanger:edit")
	@RequestMapping(value = "delete")
	public String delete(Goods goods, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			model.addAllAttributes(goodsStoreMangerService.del(goods));
		}catch(Exception ex){
			logger.info("delete",ex);
		}
		return this.list(goods, request, response, model);
	}

	@RequiresPermissions("sys:goodsstoremanger:edit")
	@RequestMapping(value = "batchDelete")
	public String batchDelete(Goods goods,String[] arrayId, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			goodsStoreMangerService.batchDel(arrayId);
		}catch(Exception ex){
			logger.info("batchDelete",ex);
		}
		return this.list(goods, request, response, model);
	}

	@RequiresPermissions("sys:goodsstoremanger:view")
	@RequestMapping(value = "view")
	public String view(Goods goods, Model model) {
		User user = UserUtils.getUser();
		Materiel materiel = new Materiel();
		Goods resultGoods = new Goods();
		if( goods.getId() != null){
			resultGoods = goodsStoreMangerService.get(goods);
			Materiel queryMateriel = new Materiel();
			queryMateriel.setCode(resultGoods.getMaterielCode());
			materiel = materielService.get(queryMateriel);
		}
		List<Store> storeList = storeService.findStoreBySysUser(user);
		model.addAttribute("goods", resultGoods);
		model.addAttribute("materiel", materiel);
		model.addAttribute(STORELIST, storeList);
		return "modules/sys/goodsstoremanger/goodsstoremangerView";
	}
	
	@RequiresPermissions("sys:goodsplatformmanger:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(Goods goods, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "门店商品管理"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            List<Goods> list = goodsStoreMangerService.findExportList(goods);
    		new ExportExcel("门店商品管理", GoodsStoreManger.class).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception ex) {
			logger.info("exportFile",ex);
			addMessage(redirectAttributes, "导出用户失败！失败信息："+ex.getMessage());
		}
		return null;
    }
}